package com.wangnian.wangcai.controller;

import com.wangnian.wangcai.config.Config;
import com.wangnian.wangcai.controller.advice.Result;
import com.wangnian.wangcai.service.BusinessService;
import com.wangnian.wangcai.service.api.ApiChannel;
import com.wangnian.wangcai.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("api")
public class Controller {
    @Autowired
    private BusinessService businessService;

    @RequestMapping("/getConfig")
    public Result getConfig() {
        return Result.success(Config.configMap);
    }

    @RequestMapping("/setConfig")
    public Result setConfig(String name, Integer value) {
        System.out.println(value);
        Config.setConfig(name, value);
        return Result.success("");
    }

    @GetMapping("/getChiGuList")
    public Result getChiGuList() {
        return Result.success(businessService.getChiGuList());
    }

    @GetMapping("/buy")
    public Result buy(String code, String name, int count) {
        ApiChannel.apiService.buy(code, count, 0, name);
        return Result.success("");
    }

    @GetMapping("/sell")
    public Result sell(String code, String name, int count) {
        ApiChannel.apiService.sell(code, count, name);
        return Result.success("");
    }

    @GetMapping("/getDp")
    public Result getDp() {
        return Result.success(businessService.DPBuyList());
    }

    @GetMapping("/getNews")
    public Result getNews() {
        String body = HttpUtil.get("http://news.10jqka.com.cn/pclient/news/push/stock/1.json");
        return Result.success(body);
    }

    @GetMapping("/getWatchChaoDi")
    public Result getWatchChaoDi() {
        return Result.success(businessService.getNameInfoS());
    }

    @GetMapping("/getTRecording")
    public Result getTRecording() {
        return Result.success(businessService.getTRecording());
    }
}
